package edu;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by leovo on 2018/5/17.
 */
public class    Database {
    public List<User> getUserList() throws SQLException, ClassNotFoundException {
        //数据库中读取数据放在list集合中
        List<User>  userList=new ArrayList <>();
        // 3+1读取数据库数据保存到list中


        Connection connection=null;
        java.sql.Statement statement=null;
        ResultSet resultSet=null;

        String sql="select * from t_users";

        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            }
            connection=DriverManager.getConnection("jdbc:mysql;//localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
                    "root",
                    "sctu123456");

        statement=connection.createStatement();
        resultSet=statement.executeQuery(sql);
        while (resultSet.next()){
            int id=resultSet.getInt(1);
            String name=resultSet.getString(2);
            String password=resultSet.getString(3);

            User user=new User();
            user.setId(id);
            user.setName(name);
            user.setPassword(password);

            userList.add(user);
        }
        resultSet.close();
        connection.close();


    } catch(SQLException e){e.printStackTrace();
    }
    return userList;
}
}
